<?php

class ThuController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }
   
        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];
 
            $result = $this->deleteThu($values);
           
            
            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                 echo'OK';
            
            }
             exit();
        } else if ($action == 'add') {
            $TRUONG_ID=$_POST['TRUONG_ID'];
            $XUAT_NGAY_XUAT=$_POST['XUAT_NGAY_XUAT'];
            $XUAT_LY_DO_XUAT=$_POST['XUAT_LY_DO_XUAT'];
            $XUAT_TONG_TIEN_PHAI_THU=$_POST['XUAT_TONG_TIEN_PHAI_THU'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
            $result = $this->addThu($TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                }
            }
             exit();
             
            
        } else if ($action == 'edit' ) {

            $XUAT_ID=$_POST['XUAT_ID'];
            $TRUONG_ID=$_POST['TRUONG_ID'];
            $XUAT_NGAY_XUAT=$_POST['XUAT_NGAY_XUAT'];
            $XUAT_LY_DO_XUAT=$_POST['XUAT_LY_DO_XUAT'];
            $XUAT_TONG_TIEN_PHAI_THU=$_POST['XUAT_TONG_TIEN_PHAI_THU'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
             $result1 = $this->deleteChiTietThu($XUAT_ID);
            $result2 = $this->deleteThu_chitiet($XUAT_ID);
            $result = $this->updateThu($XUAT_ID,$TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                }
            }
             exit();
            
            

            
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_thu' . DS . 'views' . DS . 'thu_add.php');
            
            $truongs=$this->getAllTruong();
            
              if ($truongs != null) {
                 
                foreach ($truongs as $truong) {

                     
                     $xtemplate->assign('TRUONG_ID', $truong->getTRUONG_ID());
                    $xtemplate->assign('TRUONG_TEN_TRUONG', $truong->getTRUONG_TEN_TRUONG());
                    $xtemplate->parse('main.truong');
                    
                }
            }
           $hanghoa = $this->getAllHangHoa();


            if ($hanghoa != null) {
                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());        
                    
                    $xtemplate->parse('main.hanghoa');
 
            }
            
            $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {

                    $xtemplate->assign('H_ID', $hanghoas_gia_soluongs->getID());
                    $xtemplate->assign('H_TEN', $hanghoas_gia_soluongs->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoas_gia_soluongs->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoas_gia_soluongs->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoas_gia_soluongs->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoas_gia_soluongs->getDON_GIA_LE());
                    $xtemplate->parse('main.hanghoas_gia_soluong');
                    
               
            }

            
             $xtemplate->assign('DATE', Utility::getCurrentDate());
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idthu'];
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_thu' . DS . 'views' . DS . 'thu_edit.php');
            $thu = $this->getThuById($id);
//            Utility::messageBox($id);

            if ($thu != null) {
                
 
                    $xtemplate->assign('XUAT_ID', $thu->getXUAT_ID());
                    $xtemplate->assign('TRUONG_ID', $thu->getTRUONG_ID());
                    $xtemplate->assign('XUAT_NGAY_XUAT', $thu->getXUAT_NGAY_XUAT());
                    $xtemplate->assign('XUAT_SO_PHIEU_XUAT', $thu->getXUAT_SO_PHIEU_XUAT());
                    $xtemplate->assign('XUAT_LY_DO_XUAT', $thu->getXUAT_LY_DO_XUAT());
                    $xtemplate->assign('XUAT_TONG_TIEN_PHAI_THU', Utility::formatToCurrencyString($thu->getXUAT_TONG_TIEN_PHAI_THU()));
                     $truongs=$this->getAllTruong();
             $option_truong='';
              if ($truongs != null) {
                 
                foreach ($truongs as $truong) {

                     
                      if($thu->getTRUONG_ID()==$truong->getTRUONG_ID()){
                         $xtemplate->assign('TRUONG', $truong->getTRUONG_TEN_TRUONG());
                          
                      }
                          
                    
                }
            }
                    
                    
                   
                    
                    
                    
                    
           $hanghoa = $this->getAllHangHoa();
            if ($hanghoa != null) {
                 
                
                    
                    $chitietthu=$this->getChiTietThuById($thu->getXUAT_ID(),$hanghoa->getID());
                    if($chitietthu!=null){
                        $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',Utility::formatToCurrencyString($chitietthu->getDON_GIA()) );
                    $xtemplate->assign('SO_LUONG',$chitietthu->getSO_LUONG());
                    }else{
                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',Utility::formatToCurrencyString($hanghoa->getDON_GIA_NHAP()) );
                    $xtemplate->assign('SO_LUONG',0);
                    }
                    
                    
                    $xtemplate->parse('main.thu.hanghoa');
                    
                
            }
                    
                  $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {
                 
                

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    $xtemplate->parse('main.thu.hanghoas_gia_soluong');
                    
                
            }  
                    
                    
                    $xtemplate->parse('main.thu');
                   
                
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
            
            
        } 
        else {
            $viewFile = $dir . '/views/thu.php';
            $xtemplate = new XTemplate($viewFile);
            $xuats = $this->getAllXuat();


            if ($xuats != null) {
                foreach ($xuats as $xuat) {

                    $xtemplate->assign('XUAT_ID', $xuat->getXUAT_ID());
                     $xtemplate->assign('XUAT_NGAY_XUAT',Utility::formatDateTimeToView($xuat->getXUAT_NGAY_XUAT()) );
                    $xtemplate->assign('TRUONG', $this->getTruongById($xuat->getTRUONG_ID())->getTRUONG_TEN_TRUONG());
                    $xtemplate->assign('XUAT_TONG_TIEN_PHAI_THU',Utility::formatToCurrencyString($xuat->getXUAT_TONG_TIEN_PHAI_THU()) );
                     $xtemplate->assign('XUAT_TONG_TIEN_DA_THU',Utility::formatToCurrencyString($xuat->getXUAT_TONG_TIEN_DA_THU()) );
                    
                    
                    
                    
                    $xtemplate->parse('main.xuat');
                   
                }
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        
        
        
        
        
        
        
        
    }

 
     private function getHoaHongById($id,$truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getByID($id,$truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getArrayHoaHongById($truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getArrayByID($truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
   
     private function  getAllLoaiHoaHong() {
        $db = new Database();
        try {
            $loaihoahong = new LoaiHoaHong();

            $result = $loaihoahong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
     
    
    
    
   
    
        private function updateThu($XUAT_ID,$TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
            
                $xuat = new Xuat();
                $xuat->setXUAT_ID($XUAT_ID);
                 $xuat->setTRUONG_ID($TRUONG_ID);
                
                 $date = new DateTime($XUAT_NGAY_XUAT);
                 $xuat->setXUAT_NGAY_XUAT($date->format("Y-m-d"));
                 $xuat->setXUAT_LY_DO_XUAT($XUAT_LY_DO_XUAT);
                 $xuat->setXUAT_TONG_TIEN_PHAI_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setXUAT_TONG_TIEN_DA_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setIS_ACTIVE(1);
                $xuat->update($db);
//                echo 'aa';
//            exit();
                $thu=new Thu();
                $thu->setNGAY_THU($date->format("Y-m-d"));
                $thu->setPHIEU_XUAT_ID($xuat->getXUAT_ID());
                $thu->setSO_TIEN($XUAT_TONG_TIEN_PHAI_THU);
                $thu->setIS_ACTIVE(1);
                $thu->insert($db);
                
                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietxuat=new Chi_tiet_xuat();
                $chitietxuat->setXUAT_ID($xuat->getXUAT_ID());
                $chitietxuat->setHH_ID($ID_HH_[$i]);
                $chitietxuat->setDON_GIA($DON_GIA_[$i]);
                $chitietxuat->setSO_LUONG($SOLUONG_[$i]);
                $chitietxuat->setTHANH_TIEN($THANHTIEN_[$i]);
                $chitietxuat->setIS_ACTIVE(1);
                $chitietxuat->insert($db);
            }
                
                
               
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    

    
    
    
   //----------------------đang sử dụng 
   
    private function deleteChiTietThu($values) {
        $db = new Database();
        try {            
           
                $chitietxuat = new Chi_tiet_xuat();
                $chitietxuat->setXUAT_ID($values);
                $chitietxuat->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    private function deleteThu_chitiet($values) {
        $db = new Database();
        try {            
           
                $thu = new Thu();
                $thu->setXUAT_ID($values);
                $thu->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
      private function getChiTietThuById($thu,$hanghoa) {
        $db = new Database();
        try {
            $chitietthu = new Chi_tiet_xuat();
            $result = $chitietthu->getByID($thu,$hanghoa,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
       private function getAllXuat() {
        $db = new Database();
        try {
            $xuat= new Xuat();

            $result = $xuat->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
  private function getThuById($id) {
        $db = new Database();
        try {
            $xuat = new Xuat();
            $xuat->setXUAT_ID($id);
            $result = $xuat->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
      private function getTruongById($id) {
        $db = new Database();
        try {
            $truong = new Truong();
            $truong->setTRUONG_ID($id);
            $result = $truong->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
     private function addThu($TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
            
                $xuat = new Xuat();
                $xuat->setTRUONG_ID($TRUONG_ID);
                
                 $date = new DateTime($XUAT_NGAY_XUAT);
                 $xuat->setXUAT_NGAY_XUAT($date->format("Y-m-d"));
                 $xuat->setXUAT_LY_DO_XUAT($XUAT_LY_DO_XUAT);
                 $xuat->setXUAT_TONG_TIEN_PHAI_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setXUAT_TONG_TIEN_DA_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setIS_ACTIVE(1);
                $xuat->insert($db);

               
                
                
                $thu=new Thu();
                $thu->setNGAY_THU($date->format("Y-m-d"));
                $thu->setPHIEU_XUAT_ID($xuat->getXUAT_ID());
                $thu->setSO_TIEN($XUAT_TONG_TIEN_PHAI_THU);
                $thu->setIS_ACTIVE(1);
                $thu->insert($db);
                
                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietxuat=new Chi_tiet_xuat();
                $chitietxuat->setXUAT_ID($xuat->getXUAT_ID());
                $chitietxuat->setHH_ID($ID_HH_[$i]);
                $chitietxuat->setDON_GIA($DON_GIA_[$i]);
                $chitietxuat->setSO_LUONG($SOLUONG_[$i]);
                $chitietxuat->setTHANH_TIEN($THANHTIEN_[$i]);
                $chitietxuat->setIS_ACTIVE(1);
                $chitietxuat->insert($db);
            }
                
                
               
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    
    private function getAllTruong() {
        $db = new Database();
        try {
            $truong = new Truong();

            $result = $truong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getAllHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();
            $hanghoa->setMA_HH('com');
            $result = $hanghoa->getByMA_HH($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
        private function deleteThu($values) {
        $db = new Database();
        try {            
           
                $thu = new Xuat();
                $thu->setXUAT_ID($values);
                $thu->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    
    
}
